package GUI.Lesson;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TextMain {
    public static void main(String[] args) {
        new MyFrame();
    }
}

class MyFrame extends Frame {
    public MyFrame() {
        TextField textField = new TextField();              //监听文本框
        add(textField);
        MyActionListener2 myActionListener2 = new MyActionListener2();      //监听文本框输入的文字
        textField.addActionListener(myActionListener2);         //按下Enter 就会触发输入框的事件
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        textField.setEchoChar('*');     //设置替换编码
        pack();
        setVisible(true);
    }
}
class MyActionListener2 implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        TextField field = (TextField) e.getSource();        //获得一些资源，返回的一个对象
        System.out.println(field.getText());
        field.setText("");      //返回完之后重设文本框内容为“”，null
    }
}
